package sample.canvasDemo;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.scene.text.Font;

/**
 * 作者：w on 2021-08-06.
 * 邮箱：y141111@126.com
 * 版本：v1.0
 */
public class CanvasDemo {
    public static final int epSize = 200;
    public static final int epOffset = epSize /2;
    public static final int radarSize = 300;
    public static final int radarOffset = radarSize /2;
    public static  int startNum = 0;
    public static void drawShapes(GraphicsContext gc) {
        gc.setFill(Color.GREEN);
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(5);
        gc.strokeLine(40, 10, 10, 40);
        gc.fillOval(10, 60, 30, 30);
        gc.strokeOval(60, 60, 30, 30);
        gc.fillRoundRect(110, 60, 30, 30, 10, 10);
        gc.strokeRoundRect(160, 60, 30, 30, 10, 10);
        gc.fillArc(10, 110, 30, 30, 45, 240, ArcType.OPEN);
        gc.fillArc(60, 110, 30, 30, 45, 240, ArcType.CHORD);
        gc.fillArc(110, 110, 30, 30, 45, 240, ArcType.ROUND);
        gc.strokeArc(10, 160, 30, 30, 45, 240, ArcType.OPEN);
        gc.strokeArc(60, 160, 30, 30, 45, 240, ArcType.CHORD);
        gc.strokeArc(110, 160, 30, 30, 45, 240, ArcType.ROUND);
        gc.fillPolygon(new double[]{10, 40, 10, 40},
                new double[]{210, 210, 240, 240}, 4);
        gc.strokePolygon(new double[]{60, 90, 60, 90},
                new double[]{210, 210, 240, 240}, 4);
        gc.strokePolyline(new double[]{110, 140, 110, 140},
                new double[]{210, 210, 240, 240}, 4);
    }

    /**
     * 实时误差显示
     * @param canvas 目标画布
     * @param xx 误差X
     * @param yy 误差Y 目前无效 填0即可
     * @param epName 设备要显示的图片。只填名称即可 radar∈[0,12]
     * @param radarNum 雷达需要显示的图片 radar∈[0,12]
     */
    public static void drawEp(Canvas canvas, double xx, double yy, String epName, int radarNum) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        double width = canvas.getWidth();
        double height = canvas.getHeight();
        double epX = (width / 2) + xx - epOffset;
        double epY = (height / 2) + yy - epOffset;
        double radarX = (width / 2) + xx - radarOffset;
        double radarY = (height / 2) + yy - radarOffset;
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(5);

        gc.clearRect(0, 0, width, height);

        gc.strokeLine(0, 0, 0, height);
        gc.strokeLine(0, height, width, height);
        gc.strokeLine(width, height, width, 0);
        gc.strokeLine(width, 0, 0, 0);

        gc.setStroke(Color.DARKGREEN);
        gc.setLineWidth(1);
        Font font=new Font("宋体",28);
        gc.setFont(font);
        gc.strokeText(String.valueOf(xx) + "cm", width - 100, 50);

        gc.setStroke(Color.RED);
        gc.setLineWidth(3);
        double singleNum = height / 25 / 2;
        startNum = startNum >= 50 ? 0 :startNum + 5;
        int n = startNum;
        for (int i = 0; i <= singleNum; i++){
            gc.strokeLine(width / 2, n, width / 2, n + 25);
            n = n +50;
        }
//        gc.strokeLine(500, 0, 500, 1000);


//        画吃豆豆
//        gc.setStroke(Color.DARKCYAN);
//        gc.setLineWidth(5);
//        gc.strokeArc( radarX, radarY, epSize, epSize, 150, 240, ArcType.ROUND);
        Image imageEp = new Image("/sample/res/" + epName);
        gc.drawImage(imageEp, epX, epY + 70);

        Image image = new Image("/sample/res/radar/" + radarNum + ".png");
        gc.drawImage(image, radarX, radarY - 70);
    }
}
